package com.bgs.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.bgs.common.R;
import com.bgs.pojo.Store;
import com.bgs.mapper.StoreMapper;
import com.bgs.pojo.vo.StoreTow;
import com.bgs.pojo.vo.ZeroToReportAndOrderDetail;
import com.bgs.service.StoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;


/**
 * <p>
 * 店铺表 服务实现类
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {
    @Resource
    private StoreMapper storeMapper;
    @Override
    public R zeroGetStoreToZeroToReportAndOrderDetail(List<ZeroToReportAndOrderDetail> zeroToReportAndOrderDetails) {
        for (int i = 0; i < zeroToReportAndOrderDetails.size(); i++) {
            System.out.println(i);
            ZeroToReportAndOrderDetail zeroToReportAndOrderDetail = storeMapper.zeroGetStoreToZeroToReportAndOrderDetail(zeroToReportAndOrderDetails.get(i));
            System.out.println(zeroToReportAndOrderDetail);
            //赋值
            //种类
            zeroToReportAndOrderDetails.get(i).setProducts(zeroToReportAndOrderDetail.getProducts());
            //用户
            zeroToReportAndOrderDetails.get(i).setBeforeUser(zeroToReportAndOrderDetail.getBeforeUser());
            //店铺
            zeroToReportAndOrderDetails.get(i).setStore(zeroToReportAndOrderDetail.getStore());
        }
        return R.ok().data("zeroToReportAndOrderDetails", zeroToReportAndOrderDetails);
    }



    /**
     * 后台对店铺审核查询
     * @param beforeUserName
     * @param storeName
     * @return
     */
    @Override
    public List<Store> TowGettableData(String beforeUserName,String storeName) {
        return storeMapper.TowGettableData(beforeUserName,storeName);
    }



    @Override
    public R zeroStoreStatus4(Integer storeId) {
        boolean b=storeMapper.zeroStoreStatus4(storeId);
        return R.ok().data("zeroStoreStatus4",b);
    }
    /**
     * 后台店铺审核通过
     * @param storeId
     * @return
     */
    @Override
    public int TowDianShen(Integer storeId) {
        return storeMapper.TowDianShen(storeId);
    }

    /**
     * 后台店铺审核未通过
     * @param storeId
     * @return
     */
    @Override
    public int TowDianShenNo(Integer storeId) {
        return storeMapper.TowDianShenNo(storeId);
    }


    @Override
    public List<Store> zeroStoreIdToStore(Integer storeId) {
        return storeMapper.zeroStoreIdToStore(storeId);
    }

    /**
     * 开启店铺
     * @param store
     * @return
     */
    @Override
    public R FourOpenShop(Store store) {
        Integer i = storeMapper.FourOpenShop(store);
        return R.ok().data("data",i);
    }

    /**
     * 上传图片到oss
     * @param file
     * @return
     */
    @Override
    public String FourOpenImg(MultipartFile file) {

         String endpoint = "oss-cn-beijing.aliyuncs.com";
         String accessKeyId = "LTAI5tDfTvA5NxSA7LnmKrgw";
         String accessKeySecret = "b2IkZ2gasijtwZuaKvhxcCDr1PojQI";
         String bucketName = "appasiaxiyou";

        OSS oss = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        try{
            //获取文件流
            InputStream inputStream = file.getInputStream();
            //获取上传文件的名字
            String oldName = file.getOriginalFilename();

            //生成新名字
            String fileName = "four"+System.currentTimeMillis();
            //取出上传文件的后缀
            String houzhui = oldName.substring(oldName.lastIndexOf("."));
            //新文件名字
            String newName = fileName + houzhui;

            //把文件按照日期分类
            //获取当前日期
            String datePath = new DateTime().toString("yyyy/MM/dd");
            //拼接
            fileName = datePath + "/" + newName;

            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
            objectMetadata.setContentType(houzhui);

            //调用oss方法实现上传
            //第一个参数  Bucket名称
            //第二个参数  上传到oss文件路径和文件名称   aa/bb/1.jpg
            //第三个参数  上传文件输入流
            oss.putObject(bucketName, fileName, inputStream, objectMetadata);
            // 默认十年不过期
            Date date = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
            String url = oss.generatePresignedUrl(bucketName, fileName, date).toString();
            //把上传之后文件路径返回
            return url.substring(0, url.indexOf("?"));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            oss.shutdown();
        }
    }

    /**
     * 被订单远程调用查看店铺信息
     * @param storeId
     * @return
     */
    @Override
    public List<Store> TowOrderStore(Integer storeId) {
        return storeMapper.TowOrderStore(storeId);
    }


    //查看当前商户的所有商品
    @Override
    public List<StoreTow> TowListCircle(Integer beforeUserId) {
        return storeMapper.TowListCircle(beforeUserId);
    }

    /**
     * 查询店铺id
     * @param id
     * @return
     */
    @Override
    public R fourStoreId(Integer id) {

        Store s = storeMapper.FourStoreId(id);

        return R.ok().data("id",s);
    }

    /**
     * 查询所有的店铺
     * @return
     */
    @Override
    public List<Store> FourStore() {
        List<Store> list = storeMapper.FourStore();
        return list;
    }
}
